package com.example.controller;

import com.example.pojo.DeviceAttribute;
import com.example.pojo.Result;
import com.example.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    /**
     * 获取设备在线状态
     */
    @GetMapping("/status")
    public Result getOnlineStatus(@RequestHeader(value = "token", required = false) String token) throws Exception {
        log.info("获取设备在线状态");

        if (token == null || token.isEmpty()) {
            return Result.error("404, 请求头不合法");
        }

        Integer status = deviceService.getDeviceOnlineStatus(token);
        return Result.success(status);
    }

    /**
     * 获取设备属性
     */
    @GetMapping("/attribute")
    public Result getAttribute(@RequestHeader(value = "token", required = false) String token) throws Exception {
        log.info("获取设备属性");

        if (token == null || token.isEmpty()) {
            return Result.error("404, 请求头不合法");
        }

        DeviceAttribute deviceAttribute = deviceService.getAttribute(token);

        return Result.success(deviceAttribute);
    }
}
